goog.provide('GLL');

goog.require('goog.net.Jsonp');

goog.require('lime.Node');
goog.require('lime.Scene');
goog.require('lime.Director');
goog.require('lime.Sprite');
goog.require('lime.userAgent');
goog.require('lime.fill.Image');

goog.require('GLL.Game');


//globel
(function()
{
	///@namspace
	var GLL = {};

/**add self to node
* @param {lime.Node} node
* @param {number=} opt_pos Position of new child.
* @return {lime.Node} object itself
*/
lime.Node.prototype.addTo = function(node, opt_pos)
{
	if (node instanceof lime.Node){
		node.appendChild(this, opt_pos);
	}
	return this;
}




})();


//-----------------------------------------------------------------



//-----------------------------------------------------------------
function GLL_InitRsltn()
{
	GLL.EVENT_MM = [goog.events.EventType.MOUSEMOVE,goog.events.EventType.TOUCHMOVE];
	GLL.EVENT_MD = [goog.events.EventType.MOUSEDOWN, goog.events.EventType.TOUCHSTART];
	GLL.EVENT_MU = [goog.events.EventType.MOUSEUP, goog.events.EventType.TOUCHEND, goog.events.EventType.TOUCHCANCEL];

	//best resolution
	GLL.Rsltn = {
		w : 800,
		h : 600,
		hw : 0, //half width
		hh : 0 //half height
	};

	if(lime.userAgent.IOS){
		GLL.Rsltn.w = 480;
		GLL.Rsltn.h = 320;		
	}else if(lime.userAgent.IPHONE4){
		GLL.Rsltn.w = 960;
		GLL.Rsltn.h = 640;
	}else if(lime.userAgent.IPAD){
		GLL.Rsltn.w = 1024;
		GLL.Rsltn.h = 768;
	}
	
	GLL.Rsltn.hw = GLL.Rsltn.w / 2;
	GLL.Rsltn.hh = GLL.Rsltn.h / 2;
}

/*@static
 * 
 */
GLL.init = function()
{
	GLL_InitRsltn();

	GLL._scenes = {};
	GLL._curScene = null;
	
	GLL._director = new lime.Director(document.body,GLL.Rsltn.w, GLL.Rsltn.h);
	GLL._director.makeMobileWebAppCapable();
	
	goog.events.listen(document.body, goog.events.EventType.CONTEXTMENU, function(e) {
		e.preventDefault(); 
	}); 

}

/*@static
 * 
 */
GLL.newScene = function(name)
{
	var scene = new lime.Scene();
	GLL._scenes[name] = scene;
}

/*@static
 * 
 */
GLL.useScene = function(name)
{
	var scene = GLL._scenes[name];
	if(!scene) throw Error('scene named:' + name + 'doesnt exist');
	GLL._director.replaceScene(scene);
	GLL._curScene = scene;
}

GLL.APPEND = 'HOLD';

GLL.addToScene = function(node, parent)
{
	//explicit assigned : NO parent
	if(parent == GLL.APPEND)return;
	//direct add to node
	if(parent instanceof lime.Node){
		parent.appendChild(node);
		return;
	}
	//name
	var scene = GLL._curScene;
	if(parent){
		scene = GLL._scenes[parent];
	}
	if(!scene) return;
	scene.appendChild(node);
}

GLL.getImageFill = function(file_id)
{
	return new lime.fill.Image(GLL.Game.getPicPathName(file_id));
}

/*@static
 *@param {null|lime.Scene|GLL.HOLD} lime_scene GLL.HOLD means do not append it 
 * to any node.
 *
 */
GLL.newImage = function(file_id, lime_scene)
{
	var sp = new lime.Sprite().setFill(GLL.getImageFill(file_id));
	GLL.addToScene(sp, lime_scene);
	return sp;
}

/*
 * mouse button type
 */
GLL.MBType = {
	LEFT:1,
	MIDDLE:2,
	RIGHT:3
};

GLL.Evt = {
	TOUCH : 'TOUCH',
	MOVE : 'MOVE',
	LEFT : 'LEFT'
};

GLL.EventMBType = function(e)
{
	var btn = e.event.button;
	var btnType = GLL.MBType.LEFT;
	if(btn == goog.events.BrowserEvent.MouseButton.RIGHT) 
		btnType = GLL.MBType.RIGHT;
	else if(btn == goog.events.BrowserEvent.MouseButton.MIDDLE)
		btnType = GLL.MBType.MIDDLE;

	return btnType;
}
